package peer.player;

import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.jdesktop.jdic.desktop.DesktopException;

public class PlayerMenuBar {
	Display display;

	static Shell shell;

	public static Menu menu , fileMenu, helpMenu;

	MenuItem fileMenuHeader, helpMenuHeader;

	MenuItem fileExitItem, fileSaveItem, helpGetHelpItem;

	Label label;

	PlayerMenuBar() {
		shell = Player.wShell;
		menu = new Menu(shell, SWT.BAR);
		iniMainMenu();
		iniFolderMenu(shell);
		
		new Thread(){
			public void run(){
				peer.FolderSynThread.synNow();
				while(true){
					if(!Player.isFullScr){
						Player.wDisplay.syncExec(new Runnable(){
							public void run(){
								closeFolderMenu();
								iniMainMenu();
								iniFolderMenu(shell);
							}
						});
						break;
					}else{
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				
			}
		}.start();
		
	}
	
	public static void iniMainMenu(){
		PlayerMainMenu.ini(shell,menu);
	}
	
	void closeFolderMenu(){
		menu.dispose();
		menu = new Menu(shell, SWT.BAR);
		if(PlayerPopMenu.menu!=null){
			PlayerPopMenu.menu.dispose();
			new PlayerPopMenu();
		}
	}
	
	

	public static void iniFolderMenu(Shell shell) {
		shell.setMenuBar(menu);
		File file = new File(peer.Config.getSynPath());
		File[] files = file.listFiles();
		if(files!=null){
			sortFiles(files);
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					addItem(files[i], menu);
					addItem(files[i], PlayerPopMenu.menu);
				}
			}
		}
	}

	static void addItem(File file, Menu menu) {
		if (file.isFile()) {
			final MenuItem menuItem = new MenuItem(menu, SWT.NONE);
			String name=file.getName();
			int index=name.lastIndexOf(".");
			if(index<0){
				index=name.length();
			}
			String itemString=name.substring(0,index);
			menuItem.setText(itemString);
			menuItem.setData(file);
			menuItem.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent arg0) {}

				@SuppressWarnings("unchecked")
				public void widgetSelected(SelectionEvent arg0) {
					File file=(File) menuItem.getData();
					if(file.getAbsolutePath().endsWith(".lv")){
						String selItem=file.getAbsolutePath();
						int selIndex=0;
						File parent=file.getParentFile();
						File[] files=parent.listFiles();
						ArrayList<String> strList=new ArrayList<String>();
						boolean b=false;
						for(int i=0;i<files.length;i++){
							if(files[i].isFile()){
								if(files[i].getAbsolutePath().endsWith(".lv")){
									strList.add(files[i].getAbsolutePath());
									b=true;
								}
							}
						}
						if(b){
							Player.listModel.clear();
						}
						String[] str2=new String[strList.size()];
						for(int i=0;i<str2.length;i++){
							str2[i]=(String) strList.get(i);
						}
						Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
						Arrays.sort(str2, cmp);
						for(int i=0;i<str2.length;i++){
							Player.listModel.addElement(str2[i]);
							if(str2[i].equals(selItem)){
								selIndex=i;
							}
						}
						Player.shareList.setSelectedIndex(selIndex);
						Player.tryPlay();
						Player.setlistFrameBound();
						if(strList.size()>1){
							Player.openPlayList();
						}
						
						
					}else{
						try {
							org.jdesktop.jdic.desktop.Desktop.open(file);
						} catch (DesktopException e) {
							//e.printStackTrace();
						}
					}
				}
				
			});
		} else {
			MenuItem bMenuItem = new MenuItem(menu, SWT.CASCADE);
			bMenuItem.setText(file.getName());

			Menu menu_2 = new Menu(bMenuItem);
			bMenuItem.setMenu(menu_2);
			File[] files = file.listFiles();
			sortFiles(files);
			for (int i = 0; i < files.length; i++) {
				if(files[i].isFile()){
					addItem(files[i], menu_2);
				}
				
			}
			for (int i = 0; i < files.length; i++) {
				if(files[i].isDirectory()){
					addItem(files[i], menu_2);
				}
				
			}
		}
	}

	@SuppressWarnings("unchecked")
	static void sortFiles(File[] files) {
		String[] str2 = new String[files.length];
		for (int i = 0; i < files.length; i++) {
			str2[i] = files[i].getAbsolutePath();
		}
		Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
		Arrays.sort(str2, cmp);

		for (int i = 0; i < files.length; i++) {
			files[i] = new File(str2[i]);
		}
	}
	

	

}
